<!DOCTYPE html>
<HTML>
<HEAD><meta name="viewport" content="width=device-width, initial-scale=1">
<Title>Multi-Stacked Bar Chart</Title>
<link type='text/css' rel='Stylesheet' href="maxchartapi.css" />
</HEAD>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">

<p class="heading0">ChartDirector 7.0 (Java Edition)</p>
<p class="heading1">Multi-Stacked Bar Chart</p>
<hr class="separator">
<div class="content">
<img src="images/multistackbar.png" width='600' height='350'>
<br><br>
This example demonstrates combining multi-bar style with stacked bar style. It also demonstrates drawing horizontal bar charts, using icons in axis labels with <a href="cdml.htm">CDML</a>, customizing legend and bar labels, controlling bar widths and centering legend box.<br><br>
A multi-stacked bar chart combines the multi-bar style with the stacked bar style by allowing each bar in a multi-bar chart to be a stacked bar. This provides two levels of data grouping. The data from the data sets are clusters based on their index position. Within each cluster, the data are grouped again to form stack bars.<br><br>
The standard multi-bar chart provides the first level of grouping. The <a href="Layer.addDataGroup.htm">Layer.addDataGroup</a> method is used to provide the second level of grouping.<br><br>
The key features demonstrated in this example are:<br><br>
<ul><li> Use <a href="Layer.addDataGroup.htm">Layer.addDataGroup</a> to start a new data group. All subsequent data sets added to the layer will belong to that group. The group ends when <a href="Layer.addDataGroup.htm">Layer.addDataGroup</a> is called again to start another new group.<br><br>
<li> Embed icons in axis labels by using <a href="cdml.htm">CDML</a> to encode the labels.<br><br>
<li> Set the bar border to <a href="Chart.Transparent.htm">Transparent</a> using <a href="Layer.setBorderColor.htm">Layer.setBorderColor</a>.<br><br>
<li> Set a 20% bar gap between the bar clusters and 0% (no gap) for bars within a bar cluster by using <a href="BarLayer.setBarGap.htm">BarLayer.setBarGap</a>.<br><br>
<li> Swap the x and y axes to create a horizontal bar chart using <a href="XYChart.swapXY.htm">XYChart.swapXY</a>.<br><br>
<li> Reserve 20% space at the right side of the plot area for the bar labels by using <a href="Axis.setAutoScale.htm">Axis.setAutoScale</a>.<br><br>
<li> Customize the aggregate bar labels by using <a href="Layer.setAggregateLabelFormat.htm">Layer.setAggregateLabelFormat</a>.<br><br>
<li> Customize the legend entries by using <a href="TextBox.setText.htm">TextBox.setText</a> of the <a href="LegendBox.htm">LegendBox</a> object.<br><br>
<li> Center the legend box under the plot area by using <a href="TextBox.setAlignment.htm">TextBox.setAlignment</a> of the <a href="LegendBox.htm">LegendBox</a> object.</ul>
</div>
<p class="heading1a">Source Code Listing</p>
<div class="content">
<b>[JSP Version]</b> jspdemo/multistackbar.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%
// The data for the bar chart
double[] data0 = {44, 55, 100};
double[] data1 = {97, 87, 167};
double[] data2 = {156, 78, 147};
double[] data3 = {125, 118, 211};

// The labels for the bar chart. The labels contains embedded images as icons.
String[] labels = {"&lt;*img=service.png*&gt;&lt;*br*&gt;Service", "&lt;*img=software.png*&gt;&lt;*br*&gt;Software",
    "&lt;*img=computer.png*&gt;&lt;*br*&gt;Hardware"};

// Create a XYChart object of size 600 x 350 pixels, using 0xe0e0ff as the background color,
// 0xccccff as the border color, with 1 pixel 3D border effect.
XYChart c = new XYChart(600, 350, 0xe0e0ff, 0xccccff, 1);

//Set search path to current JSP directory for loading icon images
c.setSearchPath(getServletConfig().getServletContext(), request);

// Add a title to the chart using 14 points Times Bold Itatic font and light blue (0x9999ff) as the
// background color
c.addTitle("Business Results 2001 vs 2002", "Times New Roman Bold Italic", 14).setBackground(
    0x9999ff);

// Set the plotarea at (60, 45) and of size 500 x 210 pixels, using white (0xffffff) as the
// background
c.setPlotArea(60, 45, 500, 210, 0xffffff);

// Swap the x and y axes to create a horizontal bar chart
c.swapXY();

// Add a title to the y axis using 11 pt Times Bold Italic as font
c.yAxis().setTitle("Revenue (millions)", "Times New Roman Bold Italic", 11);

// Set the labels on the x axis
c.xAxis().setLabels(labels);

// Disable x-axis ticks by setting the tick length to 0
c.xAxis().setTickLength(0);

// Add a stacked bar layer to the chart
BarLayer layer = c.addBarLayer2(Chart.Stack);

// Add the first two data sets to the chart as a stacked bar group
layer.addDataGroup("2001");
layer.addDataSet(data0, 0xaaaaff, "Local");
layer.addDataSet(data1, 0x6666ff, "International");

// Add the remaining data sets to the chart as another stacked bar group
layer.addDataGroup("2002");
layer.addDataSet(data2, 0xffaaaa, "Local");
layer.addDataSet(data3, 0xff6666, "International");

// Set the sub-bar gap to 0, so there is no gap between stacked bars with a group
layer.setBarGap(0.2, 0);

// Set the bar border to transparent
layer.setBorderColor(Chart.Transparent);

// Set the aggregate label format
layer.setAggregateLabelFormat("Year {dataGroupName}\n{value} millions");

// Set the aggregate label font to 8 point Arial Bold Italic
layer.setAggregateLabelStyle("Arial Bold Italic", 8);

// Reverse 20% space at the right during auto-scaling to allow space for the aggregate bar labels
c.yAxis().setAutoScale(0.2);

// Add a legend box at (310, 300) using TopCenter alignment, with 2 column grid layout, and use 8pt
// Arial Bold Italic as font
LegendBox legendBox = c.addLegend2(310, 300, 2, "Arial Bold Italic", 8);
legendBox.setAlignment(Chart.TopCenter);

// Set the format of the text displayed in the legend box
legendBox.setText("Year {dataGroupName} {dataSetName} Revenue");

// Set the background and border of the legend box to transparent
legendBox.setBackground(Chart.Transparent, Chart.Transparent);

// Output the chart
WebChartViewer viewer = new WebChartViewer(request, "chart1");
viewer.setChart(c, Chart.SVG);

// Include tool tip for the chart
viewer.setImageMap(c.getHTMLImageMap("", "",
    "title='Year {dataGroupName} {dataSetName} {xLabel} Revenue: {value} millions'"));
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Multi-Stacked Bar Chart&lt;/title&gt;
    &lt;!-- Include ChartDirector Javascript Library to support chart interactions --&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
    &lt;div style="font:bold 18pt verdana;"&gt;
        Multi-Stacked Bar Chart
    &lt;/div&gt;
    &lt;hr style="border:solid 1px #000080; background:#000080" /&gt;
    &lt;div style="font:10pt verdana; margin-bottom:1.5em"&gt;
        &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- ****** Here is the chart image ****** --&gt;
    &lt;%= viewer.renderHTML(response) %&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div>
</div>
<br><hr class="separator"><div class="copyright">&copy; 2022 Advanced Software Engineering Limited. All rights reserved.</div>
</body>
</HTML>
